package com.example.sunming.optional;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Optional;

/**
 * @author: sunming
 * @date: 2020/3/26
 * @description:
 */
public class OptionalDemo {

    public static void main(String[] args) {

        Optional<Member> optional = getMemberByIdFromDB();
        optional.ifPresent(mem -> {
            System.out.println(mem.getName());
        });

        String password = "123";
        Optional<String> opt = Optional.ofNullable(password);
        System.out.println(opt.filter(pwd -> pwd.length() > 6).isPresent());
    }

    public static Optional<Member> getMemberByIdFromDB() {
        boolean hasName = true;
        if (hasName) {
            return Optional.of(new Member("sunming"));
        }
        return Optional.empty();
    }

}

@Data
@AllArgsConstructor
class Member {
    private String name;

    public String getName() {
        return name;
    }
}
